import copy


class Solution(object):
    def maxKilledEnemies(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        m = len(grid)
        n = len(grid[0])
        max_value = 0
        for i in range(m):
            j = 0
            now_set = set()
            now_count = 0
            while j < n:
                if grid[i][j] == '0':
                    now_set.add((i, j))
                else:
                    if grid[i][j] == 'E':
                        now_count += 1
                    else:
                        for item in now_set:
                            grid[item[0]][item[1]] = now_count
                        now_set = set()
                        now_count = 0
                j += 1
            for item in now_set:
                grid[item[0]][item[1]] = now_count
        for j in range(n):
            i = 0
            now_set = set()
            now_count = 0
            while i < m:
                if grid[i][j] not in ['E', 'W']:
                    now_set.add((i, j))
                else:
                    if grid[i][j] == 'E':
                        now_count += 1
                    else:
                        for item in now_set:
                            grid[item[0]][item[1]] += now_count
                            max_value = max(max_value, grid[item[0]][item[1]])
                        now_set = set()
                        now_count = 0
                i += 1
            for item in now_set:
                grid[item[0]][item[1]] += now_count
                max_value = max(max_value, grid[item[0]][item[1]])
        return max_value


data = Solution()
grid = [["0", "E", "0", "0"], ["E", "0", "W", "E"], ["0", "E", "0", "0"]]
print(data.maxKilledEnemies(grid))
grid = [["W", "W", "W"], ["0", "0", "0"], ["E", "E", "E"]]
print(data.maxKilledEnemies(grid))
grid = [['0']]
print(data.maxKilledEnemies(grid))
grid = [["0"], ["E"], ["0"], ["W"]]

print(data.maxKilledEnemies(grid))
